package main

import (
	"bufio"
	"fmt"
	"net"
	"os"
	"strings"
)

func test02() {
	conn, err := net.Dial("tcp", "0.0.0.0:8888")
	if err != nil {
		fmt.Printf("dial err =%v \n", err)
		return
	}
	fmt.Printf("conn suc = %v \n", conn)
	for {
		//功能一:客户端可以发送单行数据，然后就退出
		reader := bufio.NewReader(os.Stdin) //os.stdin 代表标准输入
		//从终端读取一行用户输入，并准备发给服务器
		line, err := reader.ReadString('\n')
		if err != nil {
			fmt.Printf("feadString err = %v \n", err)
		}
		line = strings.Trim(line, "\r\n")
		if line == "exit" {
			break
		}
		//将数据发送到服务端
		n, err := conn.Write([]byte(line + "\n"))
		if err != nil {
			fmt.Printf("conn.write err=%v \n", err)
		}
		fmt.Printf("客服端发送了 %d 字节数据 \n", n)
	}
}

func main() {
	test02()
}
